#if UNITY_EDITOR
using Net.Helper;
using System.IO;
using UnityEditor;
using UnityEngine;
using static MVC.View.FieldCollectionEntity;

namespace MVC.View
{
    public class Tools
    {
        [MenuItem("CONTEXT/FieldCollection/SetFieldValueInChilds")]
        private static void SetFieldValueInChilds(MenuCommand menuCommand)
        {
            var fc = menuCommand.context as FieldCollection;
            for (int i = 0; i < fc.fields.Count; i++)
            {
                var fieldTarget = fc.fields[i].target;
                Transform transform = null;
                if (fieldTarget is GameObject gameObject)
                    transform = gameObject.transform;
                else if (fieldTarget is Component component)
                    transform = component.transform;
                if (transform == null)
                    continue;
                if (transform.GetComponent<FieldCollection>() == null) //如果引用的组件挂在根物体上, 则不能往上搜索
                {
                    var path = transform.name;
                    while (transform.parent != null)
                    {
                        transform = transform.parent;
                        if (transform.GetComponent<FieldCollection>() != null)
                            break;
                        path = transform.name + "/" + path;
                    }
                    transform = fc.transform.Find(path);
                }
                else
                {
                    transform = fc.transform;
                }
                if (fieldTarget is GameObject)
                    fc.fields[i].target = transform.gameObject;
                else
                    fc.fields[i].target = transform.GetComponent(fieldTarget.GetType());
            }
            EditorUtility.SetDirty(fc);
        }

        [MenuItem("GameDesigner/MVC/旧版本转换")]
        private static void Convert()
        {
            var data = PersistHelper.Deserialize<FieldCollectData>("fcdata.json");
            CheckMVCScript(data.view);
            AssetDatabase.Refresh();
            Debug.Log("转换完成!");
        }

        private static void CheckMVCScript(FCData data)
        {
            foreach (var path in data.filePaths)
            {
                var csFiles = Directory.GetFiles(path, "*.cs");
                foreach (var csFile in csFiles)
                {
                    var className = Path.GetFileNameWithoutExtension(csFile);
                    var files = Directory.GetFiles(Application.dataPath, $"{className}Ex*.cs", SearchOption.AllDirectories);
                    if (files.Length == 0)
                        continue;
                    byte[] bytes;
                    var filePath = $"{path}/{className}.cs";
                    if (File.Exists(filePath))
                    {
                        bytes = File.ReadAllBytes(filePath);
                        var designerFile = $"{path}/{className}.Designer.cs";
                        File.WriteAllBytes(designerFile, bytes);
                    }
                    bytes = File.ReadAllBytes(files[0]);
                    File.WriteAllBytes(filePath, bytes);
                    File.Delete(files[0]);
                }
            }
        }
    }
}
#endif